package com.pianzhu.blockchainemr.service.impl;

import com.pianzhu.blockchainemr.mapper.EnrollMapper;
import com.pianzhu.blockchainemr.pojo.EmrDeptDict;
import com.pianzhu.blockchainemr.pojo.EmrHospital;
import com.pianzhu.blockchainemr.pojo.EmrPatMaster;
import com.pianzhu.blockchainemr.pojo.Param.EnrollParam;
import com.pianzhu.blockchainemr.service.EnrollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EnrollServiceImpl implements EnrollService {
    @Autowired
    private EnrollMapper enrollMapper;


    //    /**
    //     * 插入医生： 1。新建用户，传入username，password，name，usertype,hospitalId；
    //     * 插入患者： 1。新建用户，传入username，password，name，usertype；
    //     *          2. 插入到emr_pat_master表中，传入username，name，hospitalId，deptCode。
    //     */
    @Override
    @Transactional
    public Boolean enroll(EnrollParam enrollParam) {
        switch (enrollParam.getUsertype()) {
            case "doctor":
                // 新建医生
                enrollMapper.insertDoctor(enrollParam);
                break;
            case "patient":
                enrollMapper.insertPatient(enrollParam);
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public List<EmrHospital> getHospitalList() {
        List<EmrHospital> hospitalList = enrollMapper.getHospitalList();
        return hospitalList;
    }

    @Override
    public List<EmrDeptDict> getDeptList() {
        List<EmrDeptDict> deptList = enrollMapper.getDeptList();
        return deptList;
    }

    @Override
    @Transactional
    public void savePatMaster(EmrPatMaster emrPatMaster) {
        Integer id = enrollMapper.selectIdByUsername(emrPatMaster.getNamePhonetic());
        emrPatMaster.setUserId(id);
        enrollMapper.insertPatMaster(emrPatMaster);
    }
}
